﻿using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Repositories;

namespace HMIS.Infrastructure.Core.Authority.Services.Impl
{
    public class AuthorityServiceImpl : IAuthorityService
    {
        private readonly IHospUserRepository HospUserRepository;

        public AuthorityServiceImpl(IHospUserRepository HospUserRepository)
        {
            this.HospUserRepository = HospUserRepository;
        }

        #region IAuthorityService 成员

        public void ChangeUserPwd(string userId, string oldPwd, string newPwd)
        {
            if (string.IsNullOrEmpty(oldPwd) || string.IsNullOrEmpty(newPwd))
            {
                throw new ValidationException("旧密码和新密码都不允许为空！");
            }

            if (oldPwd == newPwd)
                throw new ValidationException("新密码不能与旧密码相同！");

            HospUser user = HospUserRepository.Get(userId);

            if (user.Pwd != HospUser.GetPwdMD5(oldPwd))
                throw new ValidationException("旧密码不正确！");

            user.Pwd = HospUser.GetPwdMD5(newPwd);
            HospUserRepository.SaveOrUpdate(user);
        }

        #endregion
    }
}
